package com.springmvchomework.controller;

import com.springmvchomework.pojo.Student;
import com.springmvchomework.service.StudentService;
import com.springmvchomework.service.StudentServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 从卿
 * @create 2022/4/12-20:56
 */
@Controller
public class StudentController {
    /**
     * 插入与查询学生操作
     */
    @RequestMapping(value = "/insertStudent.do", method = RequestMethod.GET)
    public ModelAndView insertStudent(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        StudentService studentService = new StudentServiceImpl();
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String address = request.getParameter("address");
        Student student = new Student(name, age, address);
        int i = studentService.addStudent(student);
        if (i > 0) {
            mv.addObject("suceess", "插入成功");
            mv.setViewName("studentInsert");
        }

        return mv;
    }

    /**
     * 预更新学生操作
     *
     * @return
     */
    @RequestMapping(value = "/preUpdateStudent.do", method = RequestMethod.GET)
    public ModelAndView preUpdateStudent(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        StudentService studentService = new StudentServiceImpl();
        String name = request.getParameter("name");
        Student stu = studentService.preUpdateStu(name);
        mv.addObject("stu", stu);
        mv.setViewName("studentUpdate");
        return mv;
    }

    /**
     * 更新学生操作
     *
     * @return
     */
    @RequestMapping(value = "/updateStudent.do", method = RequestMethod.GET)
    public ModelAndView updateStudent(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        StudentService studentService = new StudentServiceImpl();
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        Student stu = new Student(name, address);
        int i = studentService.updateStu(stu);
        if (i > 0) {
            mv.addObject("stu", stu);
            mv.addObject("yes", "修改地址成功");
            mv.setViewName("studentUpdate");
        }
        return mv;


    }
    /**
     * 查询学生操作
     *
     * @return
     */
    @RequestMapping(value = "/selectStudent.do", method = RequestMethod.GET)
    public ModelAndView selectStudent(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        StudentService studentService = new StudentServiceImpl();
        List<Student> studentList = studentService.showStu();
        mv.addObject("studentList", studentList);
        mv.setViewName("studentSelect");

        return mv;


    }
}
